package mwt.xml.xdbforms.schemalayer.types;

import java.sql.Timestamp;
import javax.xml.XMLConstants;
import mwt.xml.xdbforms.dblayer.metadata.impl.ColumnMetaData;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;


/**
 * 
 *
 * @author Gianfranco Murador, Matteo Ferri, Cristian Castiglia
 */
public class XmlTimeStamp extends XmlDate {

    public XmlTimeStamp(Document doc) {
        super(doc);
    }

    XmlTimeStamp() {
    }

    private void buildFragment(ColumnMetaData cmd) {

        Element xtype, restriction, pattern;

        fragment = doc.createDocumentFragment();
        typeName = cmd.getTypeName();

        xtype = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:simpleType");
        xtype.setAttribute("name", typeName);

        cmd.setXschemaTypeName(typeName);


        restriction = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:restriction");
        restriction.setAttribute("base", "xs:dateTime");

        pattern = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:pattern");

        pattern.setAttribute("value", "\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}");

        xtype.appendChild(restriction);
   //     restriction.appendChild(pattern);
        fragment.appendChild(xtype);



    }

    @Override
    public DocumentFragment getSchemaType(ColumnMetaData cmd) {
        buildFragment(cmd);
        return fragment;
    }

    @Override
    public Object getXSchemaToJavaType(String baseType, String value) {

        value = value.replace("T", " ");
        Timestamp t = Timestamp.valueOf(value+".000000000");
        return t;

    }



}

